package com.frxuexi.mymvi

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MviActivity : AppCompatActivity() {
    val vm:MviViewModel by viewModels<MviViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mvi)

        lifecycleScope.launch {
            vm.mviState.collect{
                when(it){
                    is MviState.onSuccess->{
                        Log.i("lr",it.data.toString())
                    }
                    is MviState.onError->{
                        Log.i("lr",it.msg)
                    }
                    is MviState.onLoading->{
                        Log.i("lr","开始")
                    }
                }
            }
        }

        lifecycleScope.launch {
            vm.mviChannel.send(MviIntent.getNews)
        }

        lifecycleScope.launch {
            (1..10).asFlow().filter {
                it%2==0
            }.map {
                it*3
            }.buffer().collect{
                Log.i("lr",it.toString())
            }
        }

    }
}